package com.help10000.rms.ui.activitys.mine.feedback

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.amir.common.beans.FeedBackEntity
import com.amir.common.utils.Constant
import com.amir.common.utils.ToastUtil
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.FeedbackListPresenter
import com.help10000.rms.mvp.view.FeedbackView
import com.help10000.rms.ui.adapter.FeedbackListAdapter
import com.help10000.rms.ui.base.BaseActivity
import kotlinx.android.synthetic.main.activity_feed_back_list.*

class FeedBackListActivity : BaseActivity<FeedbackListPresenter, FeedbackView>(), FeedbackView {
    override fun getlayoutId(): Int {
        return R.layout.activity_feed_back_list
    }

    var page = 1
    var mAdapter: FeedbackListAdapter? = null
    override fun initView() {
        initRefreshLayout(refresh)

        addFeedTv.setOnClickListener(DuplicateClickProxy {
            startActivity(Intent(this, AddFeedbackActivity::class.java))
        })
    }

    override fun loadMore() {
        isRefresh = true
        page++
        mPresenter.getFeedList(page)
    }

    override fun downOnRefresh() {
        isRefresh = true
        page = 1
        mPresenter.getFeedList(page)
    }

    override fun createPresenter(): FeedbackListPresenter {
        return FeedbackListPresenter(this)
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    var isRefresh = false
    override fun showLoading() {
        if (!isRefresh)
            super.showLoading()
    }

    override fun onResume() {
        super.onResume()
        mPresenter.getFeedList(page)
    }


    override fun onSuccess() {


    }

    override fun deleteSuccess(pos: Int) {
        ToastUtil.show("删除成功")
        mAdapter?.data?.removeAt(pos)
        mAdapter?.notifyItemRemoved(pos)
        mAdapter?.notifyItemRangeChanged(pos, mAdapter!!.itemCount - pos)
        if (mAdapter?.data?.isEmpty() == true) {
            tv_empty.visibility = View.VISIBLE
        }

    }

    override fun fileUploadSuccess(data: MutableList<String>) {

    }

    override fun onError(mess: String?) {
        super.onError(mess)
        isRefresh = false
    }

    override fun onDataSuccess(data: FeedBackEntity) {
        isRefresh = false
        onLoadMoreFinish(true)
        onRefreshFinish(true)
        if (data.data.FeelbackList.isEmpty() && (page == 1 || mAdapter == null)) {
            tv_empty.visibility = View.VISIBLE
            mAdapter?.data?.clear()
            mAdapter?.notifyDataSetChanged()
            return
        }
        if (page == 1) {
            tv_empty.visibility = View.GONE
            mAdapter = FeedbackListAdapter(this, data.data.FeelbackList, mPresenter)
            recyclerView.apply {
                itemAnimator = null
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter
            }

        } else {
            var oldCount = mAdapter!!.itemCount
            mAdapter?.data?.addAll(data.data.FeelbackList)
            mAdapter?.notifyItemRangeInserted(oldCount, data.data.FeelbackList.size)
            if (data.data.FeelbackList.size < Constant.PAGESIZE) {
                onFinishNoMore()
            }
        }

    }

}